const { Thumbs } = require("../model");

// 改
exports.updateRole = async (req, res, next) => {
  try {
    const fun = await Thumbs.findByIdAndUpdate(req.params.id, {
      $set: req.body
    })
    if (fun) {
      res.status(200).json({
        code: 200,
        msg: "success",
      });
    } else {
      res.status(200).json({
        code: 500,
        msg: "修改失败",
      });
    }
    
  } catch (error) {
    next(error);
  }
};

// 查
exports.getRole = async (req, res, next) => {
  try {
    // 模糊查询
    // find({name:/程序员/}) 
    const items = await Thumbs.find();
    res.status(200).json({
      code: 200,
      data: items,
    });
  } catch (error) {
    next(error);
  }
};
// 增
exports.addRole = async (req, res, next) => {
  try {
    console.log(req.body)
    const model = new Thumbs(req.body);
    await model.save();
    res.status(200).json({
      code: 200,
      msg: "success",
    });
  } catch (error) {
    next(error);
  }
};
// 删
exports.deleteRole = async (req, res, next) => {
  try {
    const items = await Thumbs.find(req.body);
    console.log(items)
    if (items.length) {
      const rs = await Thumbs.findByIdAndDelete(items[0]._id)
      if (rs) {
        res.status(200).json({
          code: 200,
          msg: '删除成功'
        });
      }
    } else {
      res.status(200).json({
        code: 500,
        msg: '操作失败'
      });
    }
  } catch (error) {
    next(error);
  }
};

